<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
<#include "/head.ftl">
</head>
<body>
<table id="dg" class="easyui-datagrid" title="角色管理"
       data-options="singleSelect:true,
			fitColumns:true,
			pagination:true,
			toolbar: '#tb',
			url:'/role/list',
			method:'get'">
    <thead>
    <tr>
        <th data-options="field:'id',width:80">ID</th>
        <th data-options="field:'name',width:80">name</th>
        <th data-options="field:'status',width:50,formatter:Common.formatStatus">status</th>
        <th data-options="field:'createTime',width:160,formatter:Common.formatDate">createTime</th>
        <th data-options="field:'updateTime',width:160,formatter:Common.formatDate">updateTime</th>
        <th data-options="field:'memo',width:100">memo</th>
    <#--<th data-options="field:'action',width:100,formatter:operation">action</th>-->
    </tr>
    </thead>
</table>
<div id="tb">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newRole()">New
        Role</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editRole()">Edit
        Role</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeRole()">Remove
        Role</a>
</div>
<!-- 添加角色 -->
<div id="dlg" class="easyui-dialog" style="width:400px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
        <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">Role Information</div>
        <div style="margin-bottom:10px">
            <input name="name" class="easyui-textbox" required="true" label="角色名:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <ul id="tt" class="easyui-tree" label="角色名:" style="width:100%"></ul>
        </div>
        <div style="margin-bottom:10px">
            <input name="memo" class="easyui-textbox" label="备注:" style="width:100%">
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveRole()" style="width:90px">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
</div>

<script type="text/javascript">
    $(function () {
        initTree();
    });

    function initTree() {
        $('#tt').combotree({
            url: '/permission/tree/',
            method: 'get',
            animate: true,
            checkbox: true,
            multiple: true
        });
    }
    function authorized(id) {
        var data = [];
        $.ajax({
            type: 'get',
            url: '/role/authorized/' + id,
//            data : "test=" + test,
            async: false,
            success: function (result) {
                if (result.success) {
                    data = result.data;
                } else {
                    $.messager.show({    // show error message
                        title: 'Error',
                        msg: result.msg
                    });
                }
            }
        });
        return data;
    }
    function setValue(data) {
        $('#tt').combotree('setValues', data);
    }
    function getValue() {
        var val = $('#tt').combotree('getValues');
        return val;
    }
    var url;
    function operation(value, row, index) {
        var str = '';
        str += '<a href="javascript:void(0)" onclick="grant();" >授权</a>';
        return str;
    }

    function newRole() {
        $('#dlg').dialog('open').dialog('center').dialog('setTitle', 'New Role');
        $('#fm').form('clear');
        $('#tt').combotree('setValues', []);
        url = '/role/save';
    }
    function editRole() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', 'Edit role');
            $('#fm').form('load', row);
            initTree();
            var data = authorized(row.id);
            setValue(data);
            url = '/role/update/' + row.id;
            getValue();
        }
    }
    function saveRole() {
        $('#fm').form('submit', {
            url: url,
            queryParams: {'ids': getValue()},
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (!result.success) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.msg
                    });
                } else {
                    $('#dlg').dialog('close');        // close the dialog
                    $('#dg').datagrid('reload');    // reload the Role data
                }
            }
        });
    }
    function removeRole() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', 'Are you sure you want to destroy this role?', function (r) {
                if (r) {
                    $.post('/role/delete/' + row.id, {id: row.id}, function (result) {
                        if (result.success) {
                            $('#dg').datagrid('reload');    // reload the Role data
                        } else {
                            $.messager.show({    // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    }, 'json');
                }
            });
        }
    }
</script>
</body>
</html>